package com.newway.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.back.entity.SysUserEntity;
import com.newway.back.mapper.SysUserMapper;
import com.newway.back.service.SysUserService;
import com.newway.back.util.SecurityUserContext;
import com.newway.common.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/back/sys/user")
public class SysUserController {

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysUserMapper sysUserMapper;


    /**
     * 登陆成功后获取用户基本信息
     * @return
     */
    @GetMapping("/userBaseInfo")
    public Result getUserBaseInfo(){
        List<String> currentUserRoles = SecurityUserContext.getCurrentUserRoles();
        List<String> currentUserPerms = SecurityUserContext.getCurrentUserPerms();
        SysUserEntity currentUserInfo = SecurityUserContext.getCurrentUserInfo();
        Map<String,Object> map = new HashMap<>();
        map.put("perms",currentUserPerms);
        map.put("roles",currentUserRoles);
        map.put("userInfo",currentUserInfo);
        return Result.success(map);
    }

    @GetMapping("/userList")
    public Result getSysUserList(SysUserEntity sysUserEntity){
        Page<SysUserEntity> sysUserEntityPage = new Page<>();
        sysUserEntityPage.setSize(sysUserEntity.getPageSize());
        sysUserEntityPage.setCurrent(sysUserEntity.getPageNum());
        QueryWrapper<SysUserEntity> sysUserEntityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(sysUserEntity.getUsername())){
            sysUserEntityQueryWrapper.like("username",sysUserEntity.getUsername());
        }
        Page<SysUserEntity> page = sysUserService.page(sysUserEntityPage, sysUserEntityQueryWrapper);
        return Result.success(page);
    }
}
